<?php

namespace Xrfg\Api\Trading\Home\Controllers;

use App\Http\Controllers\Controller;
use Xrfg\Api\Trading\Home\Requests\AppVersionRequest;
use Xrfg\Api\Trading\Home\Models\AppVersion;
use Xrfg\Api\Trading\Home\Resources\AppVersionResource;

class VersionController extends Controller
{
    /**
     * 获取原生APP最新版本.
     *
     * @param AppVersionRequest $request
     *
     * @return mixed
     */
    public function appVersion(AppVersionRequest $request)
    {
        $app_name = $request->input('app_name');
        $mobile_system = $request->input('mobile_system');

        $version = AppVersion::select('app_name', 'type', 'version_code', 'version_name', 'upgrade_point', 'download_url', 'apk_url', 'time')
            ->when($app_name, function ($query) use ($app_name) {
                $query->where('app_name', $app_name);
            })
            ->where('app_id', $mobile_system)
            ->where('status', 1)
            ->orderBy('id', 'DESC')
            ->first();

        if (empty($version)) {
            $this->error(90003);
        }

        return new AppVersionResource($version);
    }

    /**
     * APP版本日志.
     *
     * @param AppVersionRequest $request
     *
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function appVersionLog(AppVersionRequest $request)
    {
        $app_name = $request->input('app_name');
        $mobile_system = $request->input('mobile_system');

        $list = AppVersion::select('version_name', 'upgrade_point', 'time')
            ->when($app_name, function ($query) use ($app_name) {
                $query->where('app_name', $app_name);
            })
            ->where('app_id', $mobile_system)
            ->orderBy('id', 'DESC')
            ->get();

        return AppVersionResource::collection($list);
    }
}
